﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BestBuy.Common;
using System.Data.SqlClient;
using System.Configuration;

namespace BestBuy.DAL
{

    public class cBangTinMua
    {
        private TradeKeyDataContext context = new TradeKeyDataContext();
        private SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["TradeKeyConnectionString"].ConnectionString);
        public cBangTinMua()
        { }

        public void AddBangTinMua(BangTinMua item)
        {
            context.BangTinMuas.InsertOnSubmit(item);
            context.SubmitChanges();
        }

        public List<BangTinMua> GetAllByUserID(int UserID)
        {
            List<BangTinMua> list = new List<BangTinMua>();
            list = context.BangTinMuas.Where(u => u.IDTaiKhoan == UserID && (u.TinhTrang == (int)BangTinMuaStatus.Active || u.TinhTrang == (int)BangTinMuaStatus.Unverified)).OrderByDescending(u => u.IDBangTinMua).ToList();
            return list;
        }

        public BangTinMua GetByID(int ID)
        {
            return context.BangTinMuas.Where(u => u.IDBangTinMua == ID && u.TinhTrang==(int)BangTinMuaStatus.Active).FirstOrDefault();
        }

        public List<DanhMuc> GetChildrenOfCategory(int? parentID)
        {
            List<DanhMuc> list = new List<DanhMuc>();

            if (parentID.HasValue)
            {
                list = context.DanhMucs.Where(p => p.IDDanhMucCha == parentID).ToList();
            }
            else
            {
                list = context.DanhMucs.Where(p => !p.IDDanhMucCha.HasValue).ToList();
            }

            return list;
        }

        public DanhMuc GetCategoryByID(int ID)
        {
            return context.DanhMucs.Where(u => u.IDDanhMuc == ID).FirstOrDefault();
        }

        public bool HasChildren(int parentID)
        {
            int result = context.DanhMucs.Where(u => u.IDDanhMucCha == parentID).Count();
            if (result > 0)
                return true;
            return false;
        }
        
        public String GetCategoryString(int ID)
        {
            string str = "";

            DanhMuc lv1 = context.DanhMucs.Where(u => u.IDDanhMuc == ID).FirstOrDefault();
            DanhMuc lv2 = null;
            DanhMuc lv3 = null;

            if (lv1 != null)
            {
                lv2 = context.DanhMucs.Where(u => u.IDDanhMuc == lv1.IDDanhMucCha).FirstOrDefault();
                if (lv2 != null)
                {
                    lv3 = context.DanhMucs.Where(u => u.IDDanhMuc == lv2.IDDanhMucCha).FirstOrDefault();
                }
            }
            if (lv3 != null)// co 3 cap
            {
                str = lv3.TenDanhMuc + " >> " + lv2.TenDanhMuc + " >> " + lv1.TenDanhMuc;
            }
            else
            {
                if (lv2 != null)// co 2 cap
                {
                    str = lv2.TenDanhMuc + " >> " + lv1.TenDanhMuc;
                }
                else// co 1 cap
                {
                    str = lv1.TenDanhMuc;
                }

            }

            return str;
        }

        public void Update(BangTinMua item)
        {
            BangTinMua result = context.BangTinMuas.Single(u => u.IDBangTinMua==item.IDBangTinMua);
            if (result != null)
            {
                foreach (DanhMucMua dm in result.DanhMucMuas)
                {
                    context.DanhMucMuas.DeleteOnSubmit(dm);
                }
                context.SubmitChanges();

                result.DanhMucMuas = item.DanhMucMuas;
                result.TieuDe = item.TieuDe;
                result.TuKhoa = item.TuKhoa;
                result.NoiDung = item.NoiDung;
                result.ThoiHanDang = item.ThoiHanDang;
                result.TanSuatMuaHang = item.TanSuatMuaHang;
                result.SoLuongMua = item.SoLuongMua;
                result.IDDonVi = item.IDDonVi;
                result.KhoiLuongMuaHangNam = item.KhoiLuongMuaHangNam;
                result.IDDonViHangNam = item.IDDonViHangNam;
                result.KyHangGiaoHang = item.KyHangGiaoHang;
                result.CangDen = item.CangDen;
                result.PhuongThucThanhToan = item.PhuongThucThanhToan;

                context.SubmitChanges();
            }
        }

        public void RemoveAt(int ID)
        {
            var item = context.BangTinMuas.Where(u => u.IDBangTinMua == ID).FirstOrDefault();
            if (item != null)
            {
                item.TinhTrang = (int)BangTinMuaStatus.Deleted;
                context.SubmitChanges();
            }
        }
    }
}